package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.EmployeeQuery;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;



   /* @RequestMapping("/login")
    public String login(String paramString1,String paramString2){
        employeeService.login(paramString1,paramString2);
        return "employee/list";
    }*/

//角色属性
    //@RequiresPermissions("employee:list")
    //@RequriedPermission(name="员工页面",expression="employee:list")
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") EmployeeQuery qo, Model model){
        PageInfo pageInfo = this.employeeService.query(qo);
        model.addAttribute("result",pageInfo);
        model.addAttribute("depts",this.departmentService.list());
        return "employee/list";
    }

    //@RequiresPermissions("employee:input")
    //@RequriedPermission(name="员工编辑",expression="employee:input")
    @RequestMapping("/input")
    //@ResponseBody
    public String input(Long id,Model model) {
        model.addAttribute("depts", this.departmentService.list());
        model.addAttribute("roles",this.roleService.list());
        if (id != null) {
            model.addAttribute("entity", this.employeeService.get(id));
        }
        return "employee/input";
    }

    //@RequiresPermissions("employee:saveOrUpdate")
    //@RequriedPermission(name="员工保存或更新",expression="employee:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Employee emp,Long[] ids){
        try {
            this.employeeService.saveOrUpdate(emp,ids);
            return new JsonResult("操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败",false);
        }
    }

    //@RequiresPermissions("employee:delete")
    //@RequriedPermission(name="员工删除",expression="employee:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){
        try {
            this.employeeService.delete(id);
            return new JsonResult("操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败",false);
        }
    }


    //批量删除
    //@RequiresPermissions("employee:abtchDelete")
    // @RequriedPermission(name="员工删除",expression="employee:delete")
    @RequestMapping("/abtchDelete")
    @ResponseBody
    public JsonResult abtchDelete(Long[] ids){
        System.out.println("controller的IDS：：：："+ids.length);
        try {
            this.employeeService.abtchDelete(ids);
            return new JsonResult("操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败",false);
        }
    }

    @RequestMapping("/checkedName")
    @ResponseBody
    public Map<String,Boolean> checkedName(String name){
        Long id = employeeService.checkedName(name);
        HashMap<String,Boolean> map = new HashMap<>();
        if (id == null){
            map.put("valid",true);
            return map;
        }else {
            map.put("valid",false);
            return map;
        }
    }

    //下载
    @RequestMapping("/exportxls")
    public void exportxls(HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        Workbook workbook = new HSSFWorkbook();
        workbook = employeeService.exportXls();
        workbook.write(response.getOutputStream());
    }

    //上传要用到的参数是MultipartFile
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        try {
            employeeService.importXls(file);
            return new JsonResult("操作成功",true);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult("操作失败",false);
        }
    }

}



